package com.uidt.project.ptsj.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.uidt.common.utils.DictUtils;
import com.uidt.common.utils.StringUtils;
import com.uidt.common.utils.http.HttpUtils;
import com.uidt.common.utils.http.HttpUtilsMq;
import com.uidt.framework.redis.RedisCache;
import com.uidt.framework.web.domain.AjaxResult;
import com.uidt.project.ptsj.domain.CountItem;
import com.uidt.project.ptsj.domain.HouseInfo;
import com.uidt.project.ptsj.domain.PlaceInfo;
import com.uidt.project.ptsj.domain.PtsjRzrInfo;
import com.uidt.project.ptsj.mapper.HouseInfoMapper;
import com.uidt.project.ptsj.mapper.PlaceInfoMapper;
import com.uidt.project.ptsj.mapper.PtsjRzrInfoMapper;
import com.uidt.project.ptsj.service.IDataSyncService;
import com.uidt.project.ptsj.service.IWeChatApiService;
import org.apache.commons.codec.digest.DigestUtils;
import org.aspectj.weaver.loadtime.Aj;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.MessageDigest;
import java.util.*;
import java.util.concurrent.TimeUnit;

import static com.uidt.common.utils.sign.SignUtil.createSign;

@Service
public class WeChatApiServiceImpl implements IWeChatApiService {

    public static String appId = "wx0338063d99672410";
    public static String secret = "4d8aece9fe8ce8577a2c1295bd98c9ae";
    static String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token";
    static String geUrlLinkUrl = "https://api.weixin.qq.com/wxa/generate_urllink";

    @Autowired
    private RedisCache redisCache;

    public static void main(String[] args) {
            WeChatApiServiceImpl weChatApiService = new WeChatApiServiceImpl();

            weChatApiService.getInfo();
//            System.out.println(url);
//            dataSyncService.syncRoomInfo();
//        JSONObject params = new JSONObject();
//        params.put("limit",1);
//        params.put("start",1);
//        System.out.println(new DataSyncServiceImpl().post(FY_ACTION,params));
    }


    public String getAccessToken(){
        String access_token=null;
        // 根据小程序id和secret，获取access_token
        String para = "?grant_type=client_credential&appid="+ appId +"&secret="+secret;
        String a = HttpUtilsMq.get(tokenUrl+para);
        JSONObject jsonObject = JSONObject.parseObject(a);
        access_token = jsonObject.get("access_token").toString();
//        access_token_flash_time = new Date();
        return access_token;
    }

    @Override
    public String getUrllink(String accessToken) {
        String url = null;
        // 根据小程序id和secret，获取access_token
        String para = "?access_token="+accessToken;

        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("path","/subPages/ranking/index");

        String a = HttpUtilsMq.postJson(geUrlLinkUrl+para,jsonObject1.toJSONString());
        JSONObject jsonObject = JSONObject.parseObject(a);
        if("ok".equals(jsonObject.get("errmsg").toString())){
            url = jsonObject.get("url_link").toString();
        }

        return url;
    }


    public AjaxResult getInfo(){

        String  noncestr = UUID.randomUUID().toString();
        String timestamp = String.valueOf(new Date().getTime()/1000);
        String url = "http://172.20.10.4:8001/login?role=travel";

        String a = HttpUtilsMq.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx494c8117d3973fe6&secret=d554480a91d10e28e27d26232509b6ca");
        JSONObject jsonObject = JSONObject.parseObject(a);
        String accesstoken = jsonObject.getString("access_token");

        String a2 = HttpUtilsMq.get("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+ accesstoken +"&type=jsapi");
        JSONObject jsonObject2 = JSONObject.parseObject(a2);
        String ticket = jsonObject2.getString("ticket");
        String sha1Hash = sha1("jsapi_ticket="+ ticket+"&noncestr="+noncestr+"&timestamp="+timestamp+"&url="+url);
        System.out.println("noncestr:"+noncestr);
        System.out.println("timestamp:"+timestamp);
        System.out.println("ticket:"+ticket);
        System.out.println("sign:"+sha1Hash);
        return null;
    }

    public static String sha1(String input) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            byte[] hash = digest.digest(input.getBytes("UTF-8"));

            StringBuilder hexString = new StringBuilder();
            for (byte b : hash) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }

            return hexString.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
